package com.geekaca.d06.homework;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test1 {
    public static void main(String[] args) {
        useMap();
        System.out.println("==============================");
        useArrayList();
    }

    public static void useMap() {

        HashMap<String, String> studentHashMap = new HashMap<>();
        for (int i = 0; i < 10000000; i++) {
            studentHashMap.put("SZ2023" + (i + 1), "Tom_" + (i + 1));
        }

        long start_map = System.currentTimeMillis();
//        studentHashMap.forEach((k, v) -> System.out.println(k + ":" + v));
        // 查找学生 Tom_89898
        /*for (String stuName : studentHashMap.values()) {
            if ("Tom_89898".equals(stuName)) {
                System.out.println("找到了学生" + stuName);
                break;
            }
            System.out.println(stuName);
        }*/

        for (Map.Entry entry : studentHashMap.entrySet()) {
            String stuName = (String) entry.getValue();
            if ("Tom_8989888".equals(stuName)) {
                System.out.println("找到了学生: " + stuName);
                break;
            }
//            System.out.println(stuName);
        }

        long end_map = System.currentTimeMillis();

        System.out.println("Map 方式查找学生Tom_89898用时:" + (end_map - start_map));

    }

    public static void useArrayList() {
        List<String> stuArrayList = new ArrayList<>();
        for (int i = 0; i < 10000000; i++) {
            stuArrayList.add("Tom_" + (i + 1));
        }
//        System.out.println(stuArrayList);
        long start_array = System.currentTimeMillis();
        for (int i = 0; i < stuArrayList.size(); i++) {
            String stuName = stuArrayList.get(i);
            if ("Tom_8989888".equals(stuName)) {
                System.out.println("找到了学生: " + stuName);
            }
        }
        long end_array = System.currentTimeMillis();
        System.out.println("ArrayList 方式查找学生Tom_89898用时:" + (end_array - start_array));
    }
}
